﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace FSC.HaNamAPApplication
{
    public partial class HNAP_DocDetail : System.Web.UI.UserControl
    {
        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }

        private int docId
        {
            get
            {
                int docId = -1;
                if (Request.QueryString["did"] != null && int.TryParse(Request.QueryString["did"].ToString(), out docId))
                {
                    docId = int.Parse(Request.QueryString["did"].ToString());
                }
                return docId;
            }
        }

        protected int number = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            Document objAP = db.Documents.SingleOrDefault(a => a.Doc_ID == this.docId);
            if (objAP != null)
            {
                lblName.Text = objAP.Name;
                lblOffice.Text = objAP.Office.Name;
                lblField.Text = objAP.Field.Name;
                lblDocType.Text = objAP.DocumentType.Name;
                lblDocNumber.Text = (objAP.DocNumber != null) ? objAP.DocNumber : "";
                if (objAP.AttachFile != null)
                {
                    hplAttachFile.Text = GetNameWithExtension(objAP.AttachFile);
                    hplAttachFile.NavigateUrl = objAP.AttachFile;
                }

                BindRepeaterLegal();
            }
        }

        private string GetNameWithExtension(string fullPath)
        {
            if (fullPath.LastIndexOf("/") != -1)
            {
                return fullPath.Substring(fullPath.LastIndexOf("/") + 1, fullPath.Length - fullPath.LastIndexOf("/") - 1);
            }
            else
            {
                return fullPath;
            }
        }

        private void BindRepeaterLegal()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            var data = from a in db.AP_Documents
                       where a.Doc_ID == this.docId
                       select new
                       {
                           Name = a.AdminProcedure.Name,
                           ID = a.AP_ID
                       };
            rptAP.DataSource = data;
            rptAP.DataBind();
        }
    }
}